package jp.ac.u_tokyo.dch.action.front;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;

import jp.ac.u_tokyo.dch.core.db.DBConnection;
import jp.ac.u_tokyo.dch.dao.common.FieldMasterDao;
import jp.ac.u_tokyo.dch.model.db.FieldMaster;
import jp.ac.u_tokyo.dch.service.DaoServiceAware;
import jp.ac.u_tokyo.dch.view.Constants;

import com.opensymphony.xwork2.ActionSupport;

public class FrontPopupAction extends ActionSupport implements DaoServiceAware {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * DBコネクションオブジェクト
	 */
	private DBConnection dbConnection = null;

	private String stageList;

	private String selectField;
	private String deleteIndex;
	private List<String> fieldKeyList = new ArrayList<String>();
	private List<String> fieldKeyNameList = new ArrayList<String>();

	private String mainFieldKey;
	private String stageFieldKeyList;
	private String editIndex;
	private String sort;

	/**
	 * 初期化
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String init() throws Exception {

		FieldMasterDao dao = new FieldMasterDao(dbConnection);

		if (StringUtils.isEmpty(stageFieldKeyList)) {

			return SUCCESS;
		}

		for (String fieldKey : stageFieldKeyList.split(Constants.FIELDKEY_DEL)) {

			FieldMaster fieldMaster = dao.selectObject(fieldKey);

			fieldKeyList.add(fieldKey);
			fieldKeyNameList.add(fieldMaster.getDisplayName());
		}

		return SUCCESS;
	}

	public String add() throws Exception {

		if (mainFieldKey.equals(selectField)) {
			addFieldError("id", getText("duplicateMsg"));
			return SUCCESS;
		}

		if (fieldKeyList.contains(selectField)) {
			addFieldError("id", getText("duplicateMsg"));
			return SUCCESS;
		}

		FieldMasterDao dao = new FieldMasterDao(dbConnection);

		FieldMaster fieldMaster = dao.selectObject(selectField);

		String displayName = fieldMaster.getDisplayName();

		fieldKeyList.add(selectField);
		fieldKeyNameList.add(displayName);

		stageFieldKeyList = StringUtils.join(fieldKeyList, Constants.FIELDKEY_DEL);

		return SUCCESS;
	}

	public String del() throws Exception {

		Integer rowIndex = Integer.valueOf(deleteIndex);
		fieldKeyNameList.remove(rowIndex.intValue());
		fieldKeyList.remove(rowIndex.intValue());

		stageFieldKeyList = StringUtils.join(fieldKeyList, Constants.FIELDKEY_DEL);

		return SUCCESS;
	}



	@Override
	public void setDBConnection(DBConnection con) {
		dbConnection = con;

	}

	public List<String> getFieldKeyNameList() {
		return fieldKeyNameList;
	}

	public void setFieldKeyNameList(List<String> fieldKeyNameList) {
		this.fieldKeyNameList = fieldKeyNameList;
	}

	public String getSelectField() {
		return selectField;
	}

	public void setSelectField(String selectField) {
		this.selectField = selectField;
	}

	public String getStageList() {
		return stageList;
	}

	public void setStageList(String stageList) {
		this.stageList = stageList;
	}

	public List<String> getFieldKeyList() {
		return fieldKeyList;
	}

	public void setFieldKeyList(List<String> fieldKeyList) {
		this.fieldKeyList = fieldKeyList;
	}

	public String getDeleteIndex() {
		return deleteIndex;
	}

	public void setDeleteIndex(String deleteIndex) {
		this.deleteIndex = deleteIndex;
	}

	public String getMainFieldKey() {
		return mainFieldKey;
	}

	public void setMainFieldKey(String mainFieldKey) {
		this.mainFieldKey = mainFieldKey;
	}

	public String getStageFieldKeyList() {
		return stageFieldKeyList;
	}

	public void setStageFieldKeyList(String stageFieldKeyList) {
		this.stageFieldKeyList = stageFieldKeyList;
	}

	public String getEditIndex() {
		return editIndex;
	}

	public void setEditIndex(String editIndex) {
		this.editIndex = editIndex;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}
}
